package com.support.android.myapp3.fragment;

import android.app.DatePickerDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RemoteViews;
import android.widget.TimePicker;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.ToastQQStyle;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.support.android.myapp3.InitApplication;
import com.support.android.myapp3.R;
import com.support.android.myapp3.acitivty.BottomBarLayoutActivity;
import com.support.android.myapp3.acitivty.BottomNavigationBarActivity;
import com.support.android.myapp3.acitivty.Card1Activity;
import com.support.android.myapp3.acitivty.ConstraintLayoutActivity;
import com.support.android.myapp3.acitivty.CoordinatorLayoutActivity;
import com.support.android.myapp3.acitivty.DialogActivity;
import com.support.android.myapp3.acitivty.Draw2Activity;
import com.support.android.myapp3.acitivty.DrawActivity;
import com.support.android.myapp3.acitivty.Main2Activity;
import com.support.android.myapp3.acitivty.Main2SettingActivity;
import com.support.android.myapp3.acitivty.RecycleCardActivity;
import com.support.android.myapp3.acitivty.Setting2Activity;
import com.support.android.myapp3.acitivty.ToolBar2Activity;
import com.support.android.myapp3.acitivty.ToolBarActivity;
import com.support.android.myapp3.acitivty.TwinklingRefreshLayoutActivity;
import com.support.android.myapp3.customComponent.dialog.DownDialog;
import com.support.android.myapp3.customComponent.popup.PopupWindowFromDown;
import com.support.android.myapp3.customComponent.popup.PopupWindowFromDown2;
import com.support.android.myapp3.util.AnimationUtil;
import com.support.android.myapp3.view.MyPopWindow;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class MainFragment extends Fragment {
    @BindView(R.id.btn_toastshow)
    Button btnToastshow;

    Unbinder unbinder;
    @BindView(R.id.btn_toolbar)
    Button btnToolbar;
    private View view;
    ViewGroup container;

    LayoutInflater inflater;

    private static MainFragment fa;

    public static MainFragment getInstance() {
        if (fa == null) {
            fa = new MainFragment();
        }
        return fa;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main, container, false);
        this.container = container;
        this.inflater = inflater;

        Button btn1 = view.findViewById(R.id.add_activity2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Main2Activity.class);
                startActivity(intent);
//                Toast.makeText(getActivity(), "aaaaaaaaaaaaaaaa", Toast.LENGTH_SHORT).show();
            }
        });

        Button btn2 = view.findViewById(R.id.add_bottomebar);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BottomNavigationBarActivity.class);
                startActivity(intent);
//                Toast.makeText(getActivity(), "aaaaaaaaaaaaaaaa", Toast.LENGTH_SHORT).show();
            }
        });

        Button btn3 = view.findViewById(R.id.add_bottomLayoutbar);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), BottomBarLayoutActivity.class));
//                Toast.makeText(getActivity(), "aaaaaaaaaaaaaaaa", Toast.LENGTH_SHORT).show();
            }
        });

        unbinder = ButterKnife.bind(this, view);

        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        monthOfYear = calendar.get(Calendar.MONTH);
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);


        return view;
    }

    @OnClick(R.id.btn_toastshow)
    public void toastShow() {
//        Toast.makeText(getActivity(), "哈哈哈哈哈11111", Toast.LENGTH_SHORT).show();
        ToastUtils.initStyle(new ToastQQStyle());
        ToastUtils.show("哈哈哈哈哈！");
    }

    @OnClick(R.id.btn_toolbar)
    public void toolBarShow() {
        startActivity(new Intent(getActivity(), ToolBarActivity.class));
//        ToastUtils.show("哈哈哈哈哈！");
//        startActivity(new Intent(getActivity(), BottomBarLayoutActivity.class));
    }

    @OnClick(R.id.btn_toolbar2)
    public void toolBarShow2() {
        startActivity(new Intent(getActivity(), ToolBar2Activity.class));
//        ToastUtils.show("哈哈哈哈哈！");
//        startActivity(new Intent(getActivity(), BottomBarLayoutActivity.class));
    }

    @OnClick(R.id.btn_setting2)
    public void settingShow2() {
        startActivity(new Intent(getActivity(), Setting2Activity.class));
//        ToastUtils.show("哈哈哈哈哈！");
//        startActivity(new Intent(getActivity(), BottomBarLayoutActivity.class));
    }

    @OnClick(R.id.btn_setting3_main)
    public void settingMain2() {
        startActivity(new Intent(getActivity(), Main2SettingActivity.class));
    }

    @OnClick(R.id.btn_card1)
    public void card1() {
        startActivity(new Intent(getActivity(), Card1Activity.class));
    }

    @OnClick(R.id.btn_recycle1)
    public void recycle1() {
        startActivity(new Intent(getActivity(), RecycleCardActivity.class));
    }

    @OnClick(R.id.btn_draw)
    public void draw() {
        startActivity(new Intent(getActivity(), DrawActivity.class));
    }

    @OnClick(R.id.btn_draw2)
    public void draw2() {
        startActivity(new Intent(getActivity(), Draw2Activity.class));
    }

    @OnClick(R.id.btn_coorlayout1)
    public void coorlayout1() {
        startActivity(new Intent(getActivity(), CoordinatorLayoutActivity.class));
    }
    @OnClick(R.id.btn_constranitlayout1)
    public void constraintlayout1() {
        startActivity(new Intent(getActivity(), ConstraintLayoutActivity.class));
    }

    @OnClick(R.id.btn_dialogActivity1)
    public void dialogActivity() {
        startActivity(new Intent(getActivity(), DialogActivity.class));
    }
    @OnClick(R.id.btn_tklayout1)
    public void tklayout1() {
        startActivity(new Intent(getActivity(), TwinklingRefreshLayoutActivity.class));
    }

    @OnClick(R.id.btn_popup1)
    public void popUpCus() {
        MyPopWindow popWindow = new MyPopWindow(getActivity());
        popWindow.showPopupWindow(new View(getActivity()));
    }

    @OnClick(R.id.btn_custpopup1)
    public void popUpCus1() {
        popupcus2();
    }

    @OnClick(R.id.btn_custpopup2)
    public void popUpCus2() {
        popupcus3();
    }

    public void popupcus3(){
//        View v = View.inflate(getActivity(), R.layout.popup_view2, null);
        PopupWindowFromDown2 popupWindow = new PopupWindowFromDown2(getActivity(),
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.showPopupWindow();
    }

    public void popupcus2(){
                PopupWindowFromDown downWindow = new PopupWindowFromDown(getActivity());
        downWindow.showPopupWindow();
    }
    @OnClick(R.id.btn_popup2)
    public void popUp() {
        View view = View.inflate(getActivity(), R.layout.popup_view2, null);

        final PopupWindow  popupWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

//        final PopupWindow  popupWindow = new PopupWindow(view);
        // 设置popupwindow点击窗体外可以消失;但必须要给popupwindow设置背景
        popupWindow.setOutsideTouchable(true);
        // 给popupwindow设置背景颜色
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#99ffffff")));
        //监听
        Button toast = (Button) view.findViewById(R.id.toast);
        Button close = (Button) view.findViewById(R.id.close);

        toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("点击popupwindow内部按钮弹出的");
                popupWindow.dismiss();
            }
        });

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        showpopup_three(popupWindow,view);
    }

    public void showpopup(PopupWindow popupWindow,View v) {

        System.out.println(popupWindow.isShowing());
        // 判断popupwindow是否在显示
        if (!popupWindow.isShowing()) {
            popupWindow.showAsDropDown(v);

        }

    }

    public void showpopup_two(PopupWindow popupWindow,View v) {
        // 判断popupwindow是否在显示
        if (!popupWindow.isShowing()) {
            // 参数一:popupwindow显示时的参照控件,参数二:X轴的偏移量,参数三:Y轴的偏移量
            popupWindow.showAsDropDown(v, 100, 200);
        }

    }

    public void showpopup_three(PopupWindow popupWindow,View v) {
        // 参数一:popupwindow的父窗体.参数二:popupwindow显示时的相对位置,参数三:X轴的偏移量,参数四:Y轴的偏移量
//        popupWindow.showAtLocation(v, Gravity.LEFT, 0, 0);
        v.startAnimation(AnimationUtil.getTranslateAnimation(0, 0, 400, 0, 200));
        popupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);

    }

    @OnClick(R.id.btn_notify)
    public void genNotify() {

        Bitmap largeIcon = BitmapFactory.decodeResource(
               getActivity().getResources(), R.mipmap.apple);

        RemoteViews remoteViews = new RemoteViews(getActivity().getPackageName(), R.layout.notify_view);

        NotificationCompat.BigTextStyle textStyle = new NotificationCompat.BigTextStyle();
        textStyle.setBigContentTitle("BigContentTitle")
                .setSummaryText("SummaryText")
                .bigText("I am Big Text");

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText(
                "SummaryText");
        for (int i = 0; i < 5; i++)
            inboxStyle.addLine("news:" + i);


        NotificationCompat.BigPictureStyle pictureStyle = new NotificationCompat. BigPictureStyle();
        pictureStyle.setBigContentTitle("BigContentTitle")
                .setSummaryText("SummaryText").bigPicture(largeIcon);


        Intent mainIntent = new Intent(getActivity(), Card1Activity.class);
        PendingIntent mainPendingIntent = PendingIntent.getActivity(getActivity(), 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager notifyManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        //实例化NotificationCompat.Builde并设置相关属性
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity())
                //设置小图标
                .setSmallIcon(R.mipmap.news_icon)
                .setLargeIcon(largeIcon)
                .setContentInfo("123")
                .setTicker("Test Notify")
                //设置通知标题
                .setContentTitle("最简单的Notification")
                //设置通知内容
                .setContentText("只有小图标、标题、内容")
                .setAutoCancel(true)
                .setStyle(textStyle)
//                .setCustomContentView(remoteViews)
                .setContentIntent(mainPendingIntent);

        Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(getActivity(), Card1Activity.class);


        PendingIntent hangPendingIntent = PendingIntent.getActivity(getActivity(), 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//        builder.setFullScreenIntent(hangPendingIntent, true);
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);

        //设置通知时间，默认为系统发出通知的时间，通常不用设置
        //.setWhen(System.currentTimeMillis());
        //通过builder.build()方法生成Notification对象,并发送通知,id=1
        notifyManager.notify(1, builder.build());
//        notifyManager.notify(2, builder.build());
    }

    @OnClick(R.id.btn_applyp)
    public void applyP() {
        XXPermissions.with(getActivity())
                //.constantRequest() //可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                //.permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES) //支持请求6.0悬浮窗权限8.0请求安装权限
                .permission(Permission.Group.STORAGE, Permission.Group.CALENDAR) //不指定权限则自动获取清单中的危险权限
                .permission("android.permission.USE_FINGERPRINT")
                .request(new OnPermission() {

                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll) {
                            ToastUtils.show("获取权限成功");
                        }else {
                            ToastUtils.show("获取权限成功，部分权限未正常授予");
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        if(quick) {
                            ToastUtils.show("被永久拒绝授权，请手动授予权限");
                            //如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.gotoPermissionSettings(getActivity());
                        }else {
                            ToastUtils.show("获取权限失败");
                        }
                    }
                });
    }

    @OnClick(R.id.btn_hasp)
    public void hasP() {
        if (XXPermissions.isHasPermission(getActivity(), Permission.Group.STORAGE)) {
            ToastUtils.show("已经获取到权限，不需要再次申请了");
        }else {
            ToastUtils.show("还没有获取到权限或者部分权限未授予");
        }
    }

    @OnClick(R.id.btn_gotop)
    public void gotoP() {
        XXPermissions.gotoPermissionSettings(getActivity());
    }

    private int year, monthOfYear, dayOfMonth, hourOfDay, minute;


    @OnClick(R.id.btn_datepick)
    public void datepick() {
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {

                toastShow("日期：" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
            }
        }, year, monthOfYear, dayOfMonth);

        datePickerDialog.show();
    }

    @OnClick(R.id.btn_timepick)
    public void timepick() {
        TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                toastShow("Time: " + hourOfDay + ":" + minute);
            }
        }, hourOfDay, minute, true);

        timePickerDialog.show();
    }

    @OnClick(R.id.btn_diolog1)
    public void dd1() {
        dialog1();
    }

    public void dialog2() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//        builder.setMessage("确认退出吗？");
        builder.setTitle("提示");
        builder.setIcon(R.mipmap.apple);
//        final EditText et = new EditText(getActivity());
//        builder.setView(et);
//        View layout = inflater.inflate(R.layout.dialog1, container,false);
//        builder.setView(layout);

        final String[] mList = {"选项1", "选项2", "选项3", "选项4", "选项5", "选项6", "选项7"};

        builder.setItems(mList, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), mList[which], Toast.LENGTH_LONG)
                        .show();
            }
        });

        builder.setNeutralButton("一般", new DialogInterface.OnClickListener()

        {

            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(getActivity(), "谈不上喜欢不喜欢。", Toast.LENGTH_LONG)
                        .show();
            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener()

        {
            public void onClick(DialogInterface dialog, int which) {
//                Toast.makeText(getActivity(), et.getText(), Toast.LENGTH_LONG)
//                        .show();
                dialog.dismiss();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()

        {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dd = builder.create();
        builder.create().show();
    }


    public void dialog1() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//        builder.setMessage("确认退出吗？");
        builder.setTitle("提示");
        builder.setIcon(R.mipmap.apple);
        final EditText et = new EditText(getActivity());
//        builder.setView(et);
        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.dialog1, null);
        builder.setView(layout);

//        builder.setMultiChoiceItems(new String[]{"Item1", "Item2"}, null, null);
        builder.setSingleChoiceItems(new String[]{"Item1", "Item2"}, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.setNeutralButton("一般", new DialogInterface.OnClickListener()

        {

            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(getActivity(), "谈不上喜欢不喜欢。", Toast.LENGTH_LONG)
                        .show();
            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener()

        {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), et.getText(), Toast.LENGTH_LONG)
                        .show();
                dialog.dismiss();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()

        {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog dd = builder.create();
        dd.setContentView(layout);
        dd.show();
    }

    public void dialog3() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.dialog1, null);

        AlertDialog dd = builder.create();

        dd.show();
        dd.setContentView(layout);
    }



//    @OnClick(R.id.btn_d1)
//    public void dialogCus1() {
//        DownDialog downDialog = new DownDialog(getActivity());
//        downDialog.show();
//    }

    @OnClick(R.id.btn_mdialog1)
    public void mdialogCus1() {
        MaterialDialog.Builder builder= new MaterialDialog.Builder(getActivity());

        builder.title("Title")
        .content("11111111111111111").negativeText("取消").neutralText("一般").positiveText("确定");
        builder.show();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public void toastShow(String msg) {
//        Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG)
//                .show();
        ToastUtils.show(msg);
    }
}
